package com.jtpay.sdk.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jtpay.sdk.R;
import com.jtpay.sdk.entity.UserModel;

import java.util.List;

/**
 * Created by Administrator on 2017/8/29.
 * An adapter for the username popupwindow
 */

public class UserNamePopAdapter extends BaseAdapter {
    private Context mContext;
    private List<UserModel> mDateList;
    private ItemClickListener mListener;

    public UserNamePopAdapter(Context context, List<UserModel> list) {
        mContext = context;
        mDateList = list;
    }

    public void setListener(ItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public int getCount() {
        return mDateList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDateList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.pop_username_itme, null);
            holder.mTextView = (TextView) convertView.findViewById(R.id.username_list_tv);
            holder.mImageButton = (ImageButton) convertView.findViewById(R.id.delete_username_Ib);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.mTextView.setTag(position);
        holder.mImageButton.setTag(parent);
        holder.mTextView.setText(mDateList.get(position).getUserName());
        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.textContentClick(mDateList.get(position));
                }
            }
        });
        holder.mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.rightButtonClick(mDateList.get(position));
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        TextView mTextView;
        ImageButton mImageButton;
    }

    public interface ItemClickListener {
        void textContentClick(UserModel s);

        void rightButtonClick(UserModel remove);
    }

}
